package mapkit.layers
{
	import flash.geom.Point;

	import mapkit.carto.Envelope;
	import mapkit.carto.IMap;
	import mapkit.carto.Location;

	public class WMSTileDefinition extends TileDefinition
	{
		override public function getExtent(tile:TileInfo):Envelope
		{
			scale=Math.pow(2, tile.level);

			var lt:Location=toMapPoint(new Point(tile.col * tileWidth, tile.row *
				tileHeight));
			var rb:Location=toMapPoint(new Point((tile.col + 1) * tileWidth, (tile.row +
				1) * tileHeight));
			return new Envelope(Math.min(lt.x, rb.x), Math.min(lt.y, rb.y), Math.max(lt.x,
				rb.x), Math.max(lt.y, rb.y), extent.srid);
		}

		override public function getTileExtent(map:IMap):TileGrid
		{
			var scale:Number=extent.width / map.extent.width * map.width / tileWidth /
				colCount;
			var level:int=Math.floor(Math.LOG2E * Math.log(scale));
			if (level < 0)
			{
				level=0;
			}
			var lt:Point=fromMapPoint(new Location(map.extent.minX, map.extent.maxY));
			var rb:Point=fromMapPoint(new Location(map.extent.maxX, map.extent.minY));
			var mincol:int=Math.floor(lt.x / tileWidth);
			var minrow:int=Math.floor(lt.y / tileHeight);
			var maxcol:int=Math.floor(rb.x / tileWidth);
			var maxrow:int=Math.floor(rb.y / tileHeight);
			var offsetx:Number=mincol * tileWidth - lt.x;
			var offsety:Number=minrow * tileHeight - lt.y;
			return new TileGrid(mincol, minrow, maxcol, maxrow, level, offsetx,
				offsety);
		}

	}
}